﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace PARK.Logging
{
    public class Logger
    {
        /// <summary>
        /// 记录调试级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        public void Debug(string message)
        {
            Print(message, 0);
        }

        /// <summary>
        /// 记录调试级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="ex">异常</param>
        public void Debug(string message, Exception ex)
        {
            Print(message + "," + ex.Message.ToString(), 0);
        }

        /// <summary>
        /// 记录消息级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        public void Info(string message)
        {
            Print(message, 1);
        }

        /// <summary>
        /// 记录消息级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="ex">异常</param>
        public void Info(string message, Exception ex)
        {
            Print(message + "," + ex.Message.ToString(), 1);
        }

        /// <summary>
        /// 记录警告级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        public void Warn(string message)
        {
            Print(message, 2);
        }

        /// <summary>
        /// 记录警告级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="ex">异常</param>
        public void Warn(string message, Exception ex)
        {
            Print(message + "," + ex.Message.ToString(), 2);
        }

        /// <summary>
        /// 记录错误级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        public void Error(string message)
        {
            Print(message, 3);
        }

        /// <summary>
        /// 记录错误级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="ex">异常</param>
        public void Error(string message, Exception ex)
        {
            Print(message + "," + ex.Message.ToString(), 3);
        }

        /// <summary>
        /// 记录致命错误级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        public void Fatal(string message)
        {
            Print(message, 4);
        }

        /// <summary>
        /// 记录致命错误L级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="ex">异常</param>
        public void Fatal(string message, Exception ex)
        {
            Print(message + "," + ex.Message.ToString(), 4);
        }
        /// <summary>
        /// 记录致命错误L级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        public void Data(string message)
        {
            Print(message, 5);
        }
        /// <summary>
        /// 记录致命错误L级别的日志
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="ex">异常</param>
        public void Pay(string message)
        {
            Print(message,6);
        }
        private bool CreateLogDirectory(string strPath)
        {
            try
            {
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        private object _objLock = new object();
        private void Print(string strLogConetnt, int logType)
        {
            try
            {
                lock (_objLock)
                {
                    string rootPath = Directory.GetCurrentDirectory();
                    string strPath = $@"{rootPath}/Logger";
                    if (!Directory.Exists(strPath))
                    {
                        Directory.CreateDirectory(strPath);
                    }
                    string strLogtypePath = strPath;
                    switch (logType)
                    {
                        case 0: //调试级别
                            strLogtypePath += "\\调试级别\\";
                            break;
                        case 1: //消息级别
                            strLogtypePath += "\\消息级别\\";
                            break;
                        case 2: //警告级别
                            strLogtypePath += "\\警告级别\\";
                            break;
                        case 3: //错误级别
                            strLogtypePath += "\\错误级别\\";
                            break;
                        case 4: //致命错误
                            strLogtypePath += "\\致命错误\\";
                            break;
                        case 5: //通讯数据
                            strLogtypePath += "\\通讯数据\\";
                            break;
                        case 6: //支付
                            strLogtypePath += "\\支付日志\\";
                            break;
                    }

                    strLogtypePath += DateTime.Now.ToString("yyyy-MM-dd") + "\\";
                    if (CreateLogDirectory(strLogtypePath))
                    {
                        //写入日志
                        WriteLogger(strLogConetnt, strLogtypePath);
                    }

                }
            }
            catch
            { }
        }
        private void WriteLogger(string strLogConetnt, string strPath)
        {
            string strFile = DateTime.Now.ToString("yyyyMMdd") + ".log";
            using (FileStream fs = new FileStream(strPath + strFile, FileMode.Append))
            {
                strLogConetnt = "\r\n*************************************************************\r\n时间日期：" +
                            DateTime.Now.ToString() + "\r\n" +
                            strLogConetnt;

                byte[] buff = System.Text.Encoding.GetEncoding("gb2312").GetBytes(strLogConetnt);
                fs.Write(buff, 0, buff.Length);
            }
        }
    }
}
